package com.cactus.utils;

import com.cactus.entity.JeeUserDetails;
import com.cactus.entity.User;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public class SecurityUtils {

    public static User getCurrentUsername() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            JeeUserDetails userDetails = (JeeUserDetails)principal;
            User user = userDetails.getUser();
            return user;
        } else {
            throw new BadCredentialsException("请重新登录！");
        }
    }
}
